#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char *intro() {
    return("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n\n<html>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n<meta http-equiv=\"Content-Language\" content=\"fi\"/>\n<meta name=\"viewport\" content=\"width=device-width,user-scalable=no\" />\n\n");
}

char *outro() {
    return("\n</body>\n</html>");
}

char *nextLine(char *cp, int b, FILE *f) {
    fgets(cp, b, f);
    if (feof(f)) { return NULL; }
    else if (*cp == '\n') {
        return nextLine(cp, b, f);
    }
    else { return cp; }
}

char *replace_str(char *str, char *orig, char *rep)
{
  static char buffer[4096];
  char *p;

    if(!(p = strstr(str, orig))) {
        return str;
    }

  strncpy(buffer, str, p-str);
  buffer[p-str] = '\0';

  sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));

  return buffer;
}


char *rmln(char *l) {
    int i = 0;
    while (*l != '\0') {
        if (*l == '\n') {
            *l = '\0';
        }
        else { l++; i++; }
    }
    l -= i;
    return l;
}

char *tag(char *cp, char *t, char *att, char *attval) {
    int d = strlen(cp) + (strlen(t) * 2) + 5;
    if (att != NULL && attval != NULL) {
        d += strlen(att) + strlen(attval) + 4;
    }
    int i = 0;

    char *cq = (char *)calloc(d, sizeof(char));
    if (cq == NULL) { printf("Error allocating memory at tag\n"); return NULL; }

    *cq = '<';
    cq++; i++;
    while (*t != '\0') {
        *cq = *t;
        cq++; t++; i++;
    }
    t -= i-1;

    if (att != NULL && attval != NULL) {
        *cq = ' '; cq++; i++;
        while (*att != '\0') {
            *cq = *att;
            cq++; i++; att++;
        }
        *cq = '='; cq++; i++;
        while (*attval != '\0' && *attval != '\n') {
            *cq = *attval;
            cq++; i++; attval++;
        }
    }

    *cq = '>'; cq++; i++;

    while (*cp != '\0' && *cp != '\n') {
        *cq = *cp;
        cq++; cp++; i++;
    }

    strcat(cq, "</");
    strcat(cq, t);
    strcat(cq, ">");

    cq -= i;
    return cq;
}

char *paragraphise(char* l, char* ii) {
    if (strstr(l, ii)) {
        return(tag("", "img", "src", l));
    }
    else {
        return(tag(l, "p", NULL, NULL));
    }
}

int handleArticle(char *fn, char *l, char *title) {

    int b = 1000;
    int i = 0;

	FILE* f = fopen(fn, "r");
    if (f == NULL) { printf("Error opening %s\n", fn); return 1; }

    printf("Now processing %s\n", fn);
/*
    char *l = (char *)calloc(b, sizeof(char));
    if (l == NULL) { printf("Error allocating memory for l\n"); return 1; }
*/
    strcpy(l, rmln(fn));
    if (strstr(l, ".txt") != NULL) {
        l = replace_str(l, ".txt", ".html");
    }
    else {
        l = strcat(l, ".html");
    }

	FILE* g = fopen(l, "w");
    if (g == NULL) { printf("Error creating %s\n", l); return 1; }
    printf("- created file %s\n", l);

    fprintf(g, "%s", intro());

    l = nextLine(l, b, f);

    if (!feof(f)) {
        strcpy(title, l);
        fprintf(g, "<head><title>Pihaus - %s</title></head>\n\n", rmln(title));
        fprintf(g, "%s\n\n", tag(l, "h1", NULL, NULL));
    }

    l = nextLine(l, b, f);
    while (!feof(f)) {
        fprintf(g, "%s\n\n", paragraphise(l, "kuvatiedosto"));
        l = nextLine(l, b, f);
    }

    fprintf(g, "%s", outro());

	fclose(f);
    fclose(g);
    printf("processed!\n");
    return(0);

}

char *getIxIntro() {
    return ("<head><title>Pihaus - Sisällysluettelo</title></head>\n<body>\n\n<h1>Sisällysluettelo</h1>\n<ul>");
}

char *getIxOutro() {
    return("</ul>\n</body>\n</html>\n");
}

int main(int argc, char *argv[]) {

    int b = 50;

    char *af = (char *)calloc(b, sizeof(char));
    if (argv[1] != NULL) {
        af = argv[1];
    }
    else {
        af = "jutut.txt";
    }

    FILE *f = fopen(af, "r");
    if (f == NULL) { printf("Error opening %s\n", af); return 1; }

    FILE* ix = fopen("index.html", "w");
    if (ix == NULL) { printf("Error creating index.html\n"); return 1; }

    fprintf(ix, "%s%s\n", intro(), getIxIntro());

    char *l = (char *)calloc(b, sizeof(char));
    if (l == NULL) { printf("Error allocating memory for l\n"); return 1; }

    char *la = (char *)calloc(1000, sizeof(char));
    if (l == NULL) { printf("Error allocating memory for la\n"); return 1; }

    char *title = (char *)calloc(b, sizeof(char));
    if (l == NULL) { printf("Error allocating memory for title\n"); return 1; }

    l = nextLine(l, b, f);

    while (l != NULL) {
        handleArticle(rmln(l), la, title);
        if (title != NULL) {
            printf("title: %s\n", title);
            fprintf(ix, "<li>%s</li>\n", rmln(title));
        }

        l = nextLine(l, b, f);
    }

    printf("file ended\n");

    fprintf(ix, "%s\n", getIxOutro());

    fclose(f);
    fclose(ix);
    free(l);
    free(title);

    return(0);

}
